﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpAlgorithm
{
    class DiamondInheritance
    {
        private bool findParent = false;
        public DiamondInheritance()
        {
            int testCount = Int32.Parse(TestConsole.ReadLine());
            for (int testCase = 1; testCase <= testCount; testCase++)
            {
                findParent = false;
                //input
                int nodeCount = Int32.Parse(TestConsole.ReadLine());
                List<int>[] inherites = new List<int>[nodeCount + 1]; 
                for (int i = 1; i <= nodeCount; i++)
                {
                    string[] input = TestConsole.ReadLine().Split(' ');
                    int parentCount = Int32.Parse(input[0]);
                    inherites[i] = new List<int>();
                    for (int j = 1; j <= parentCount; j++)
                    {
                        inherites[i].Add(Int32.Parse(input[j]));
                    }
                }
                

                for (int i = 1; i <= nodeCount; i++)
                {
                    bool[] check = new bool[nodeCount + 1];
                    Search(inherites, i, check);
                }


                Console.WriteLine("Case #" + testCase + ": " + (findParent ? "Yes" : "No"));
            }
        }
        private void Search(List<int>[] inherites, int now, bool[] check)
        {
            if (findParent)
            {
                return;
            }
            check[now] = true;

            foreach (int parent in inherites[now])
            {
                if (check[parent])
                {
                    findParent = true;
                    return;
                }
                else
                {
                    Search(inherites, parent, check);
                }
            }
        }
    }
}
